package com.pda.xinma.ui.backdepot;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.pda.xinma.R;
import com.pda.xinma.adapter.BackDepotAdapter;
import com.pda.xinma.bean.BackDepotListBean;
import com.pda.xinma.bean.BackInfoBean;
import com.pda.xinma.contract.BackDepotContract;
import com.pda.xinma.presenter.BackDepotPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.config.StringKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class BackDepotAlreadyFragment extends BaseMvpFragment<BackDepotPresenter> implements BackDepotContract.View, OnRefreshListener, OnLoadMoreListener {
    @BindView(R.id.rv_content)
    RecyclerView rvContent;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    BackDepotAdapter backDepotAdapter;
    private int pager=1;
    @Override
    public void setData(BackInfoBean backInfoBean) {

    }

    @Override
    public void setList(List<BackDepotListBean> list) {
        if (pager == 1){
            backDepotAdapter.setNewInstance(list);
        }else {
            backDepotAdapter.addData(list);
        }
        srlLayout.finishRefresh();
        srlLayout.finishLoadMore();
    }

    @Override
    public void closeSuccess() {

    }

    @Override
    public void showMessage(String msg) {
        super.showMessage(msg);
        srlLayout.finishRefresh();
        srlLayout.finishLoadMore();
    }

    @Override
    public BackDepotPresenter getPresenter() {
        return new BackDepotPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_list_scan;
    }

    @Override
    public void initView() {
        backDepotAdapter=new BackDepotAdapter(R.layout.item_back_depot_list,new ArrayList<>());
        rvContent.setAdapter(backDepotAdapter);
        srlLayout.setEnableRefresh(true);
        srlLayout.setEnableLoadMore(true);
        srlLayout.setOnRefreshListener(this);
        srlLayout.setOnLoadMoreListener(this);
    }
    @Override
    public void onResume() {
        super.onResume();
        srlLayout.autoRefresh();
    }
    @Override
    public void initLinsenterner() {
        backDepotAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                    Bundle bundle=new Bundle();
                    bundle.putString(StringKey.NO,backDepotAdapter.getData().get(position).getEntryNo());
                    startIntent(AppActivityKey.BACKDEPOTDETAILACTIVITY,bundle);

            }
        });
    }

    @Override
    public void getData() {

    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        pager=1;
        presenter.selectBackList("20",pager);
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        pager++;
        presenter.selectBackList("20",pager);
    }
}
